﻿using System;   
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Facebook;
using LikeUs.WebService.Client;
using LikeUs.WebService.Client.WSInfoClient;

namespace LikeUsWebSite
{
    /// <summary>
    /// Summary description for FacebookLogin
    /// </summary>
    public class FacebookLogin : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //bool register=false;
            WSInfoProxy wsProxy = new WSInfoProxy();
            var accessToken = context.Request["accessToken"];
            context.Session["AccessToken"] = accessToken;
            int userid=wsProxy.ValidateAndCreateFBUserAccount(accessToken,100);
            UserData x=wsProxy.GetUser(userid,accessToken);
            context.Response.AppendCookie(new HttpCookie("userid",x.UserID.ToString()));
            context.Session["userData"] = x;
            context.Session.Timeout=180;
           /* var client = new FacebookClient(accessToken.ToString());
            dynamic result = client.Get("me", new { fields = "name,id,picture,email" });
            if (!Membership.ValidateUser(result.id, result.id))
            {
                register = true;
                Membership.CreateUser(result.id, result.id);
            }
            string name = result.name;
            string id = result.id;
            string thumbnail = result.picture.data.url;
            string email = result.email;
            dynamic result2 = client.Get("me", new { fields = "picture.type(large)" });
            context.Session["userName"] = id;
            context.Session["name"] = name;
            context.Session["thumbnail"] = thumbnail;
            context.Session["email"] = email;
            context.Session["picture"] = result2.picture.data.url;*/

            FormsAuthentication.SetAuthCookie(x.UserName, true);
            context.Response.Redirect("~/Learning.aspx");
        }

        public bool IsReusable
        {
            get { return false; }
        }
    }
}